// pages/allGoods/allGoods.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentSelect: "",
    allTabs: [],
    currentGoodsList: [],
    allGoodsLists: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if(options.id){
      that.setData({
        shop_id: options.id
      })
    }
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          imageUrl: app.globalData.imageUrl,
        });
      }
    });
    getClassify(that)
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // getAllGoodsByClassify(this)
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  switchTab: function (e) {
    /* if (e.target.id == 100) {
       this.setData({
         currentGoodsList: this.data.allGoodsLists
       })
     } else {*/
    if (!this.data.allTabs[e.target.dataset.index].picture) {
      getGoodsByClassify(this, e.target.id, e.target.dataset.index)
    } else {
      this.setData({
        currentGoodsList: this.data.allTabs[e.target.dataset.index].picture
      })
    }
    //  }
  }
})

function getClassify(that) {
  wx.request({
    url: app.globalData.allUrl + "/goods/clist",
    data: {
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      getGoodsByClassify(that, res.data[0].type, 0)
      // console.log(res.data)
      that.setData({
        allTabs: res.data,
      })
    }
  })
}

function getGoodsByClassify(that, id, index) {
  wx.request({
    url: app.globalData.allUrl + "/goods/shopClassGoods",
    data: {
      classify: id,
      shop_id:that.data.shop_id
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      that.data.allTabs[index].picture = res.data;
      that.setData({
        allTabs: that.data.allTabs,
        currentGoodsList: res.data,
        currentSelect: id
      })
    }
  })
}
